﻿using System;
using System.Windows;
using System.ComponentModel;

namespace SBPweb.RIA.Client.Contracts.Base
{

    public class DependencyObjectBase : DependencyObject
    {

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Methods

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected T GetTypedValue<T>(DependencyProperty property)
        {
            return (T)GetValue(property);
        }

        protected void SetTypedValue<T>(DependencyProperty property, T value)
        {
            SetTypedValue<T>(property, value, null);
        }

        protected void SetTypedValue<T>(DependencyProperty property, T value, string propertyName)
        {
            SetTypedValue<T>(property, value, propertyName, null);
        }

        protected void SetTypedValue<T>(DependencyProperty property, T value, string propertyName, EventHandler handler)
        {
            SetValue(property, value);
            if (!string.IsNullOrEmpty(propertyName))
            {
                RaisePropertyChanged(propertyName);
            }
            if (handler != null)
            {
                handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

    }

}